package com.foreveross.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.common.Constants;
import com.foreveross.dao.LogDAO;
import com.foreveross.dao.SysUserDAO;
import com.foreveross.vo.HrHouses;
import com.foreveross.vo.SysMenu;
import com.foreveross.vo.SysRole;
import com.foreveross.vo.SysRoleHouse;
import com.foreveross.vo.SysRoleMenu;
import com.foreveross.vo.SysUser;
import com.foreveross.vo.SysUserRole;

@Service("userService")
@Transactional
public class SysUserService extends BaseService {

	@Resource
	public SysUserDAO userDAO;

	@Resource
	public LogDAO logDAO;

	public List<SysUser> findUserList(String name, String account,
			int pageIndex, int pageSize) throws Exception {
		return userDAO.findUserList(name, account, pageIndex, pageSize);
	}

	public SysUser login(String account) throws Exception {
		return userDAO.login(account);
	}

	public Boolean updateUserPass(String userId, String pass) throws Exception {
		return userDAO.updateUserPass(userId, pass);
	}

	public String getUserRole(String userId) throws Exception {
		return userDAO.getUserRole(userId);
	}
	
	public List<SysRole> getUserRoleList(String userId) throws Exception{
		return userDAO.getUserRoleList(userId);
	}

	public long getUserListCount(String name, String account) throws Exception {
		return userDAO.getUserListCount(name, account);
	}

	public void deleteUserList(String userIds) throws Exception {
		for (String userId : userIds.split(",")) {
			userDAO.deleteUser(userId);
		}
	}

	public void saveUserBindRole(String userid, String roleids)
			throws Exception {
		String k[] = roleids.split(",");
		userDAO.deleteUserRole(userid);
		for (int i = 0; i < k.length; i++) {
			SysUserRole ur = new SysUserRole();
			ur.setSysRole(new SysRole(k[i]));
			ur.setSysUser(new SysUser(userid));
			userDAO.save(ur);
		}
	}

	@Transactional
	public <T> void saveOrUpdate(T entity, String type, String id)
			throws Exception {
		if (type.equals(Constants.UPDATE_ADD)) {
			userDAO.save(entity);
		} else if (type.equals(Constants.UPDATE_EDIT)) {
			userDAO.updateObject(entity, id);
		}

	}

	public void roleBindMemu(String roleid, String memuIds) throws Exception {
		String k[] = memuIds.split(",");
		userDAO.deleteRoleMemu(roleid);
		for (int i = 0; i < k.length; i++) {
			SysRoleMenu rm = new SysRoleMenu();
			rm.setSysRole(new SysRole(roleid));
			rm.setSysMenu(new SysMenu(k[i]));
			userDAO.save(rm);
		}

	}

	public void roleBindHrHouse(String roleId, String hrHouseIds)
			throws Exception {
		String k[] = hrHouseIds.split(",");
		userDAO.deleteRoleHouse(roleId);
		for (int i = 0; i < k.length; i++) {
			SysRoleHouse rh = new SysRoleHouse();
			rh.setSysRole(new SysRole(roleId));
			rh.setHrHouses(new HrHouses(k[i]));
			userDAO.save(rh);
		}
	}
	
	// 新增用户绑定楼盘
	public void userRoleBindHouse(String roleId, String hrHouseId) throws Exception {
		SysRoleHouse rh = new SysRoleHouse();
		rh.setSysRole(new SysRole(roleId));
		rh.setHrHouses(new HrHouses(hrHouseId));
		userDAO.save(rh);
	}
}
